package game.controller
{
	import game.common.NetConnect;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.ConfigManager;
	import game.managers.LayerManager;
	import game.managers.StageProxy;
	import game.managers.UIManager;
	import game.view.assignmentMenu.Assignment;
	import game.view.cooking.CookingMenu;
	import game.view.employment.EmploymentMenu;
	import game.view.mainUI.AnnounceView;
	import game.view.mainUI.InformationView;
	import game.view.mainUI.MainUI;

	public class MainUICenter extends BaseCenter
	{
		private static var _instance:MainUICenter;

		private var _mainUI:MainUI;

		private var _announceView:AnnounceView;
		private var _informationView:InformationView;
		private var _assignment:Assignment;
		private var _employmentmenu:EmploymentMenu;
		private var _cookmenu:CookingMenu;

		public function MainUICenter()
		{
			if (!_instance)
			{
				super();
				_instance=this;
				init();
				addEvent();
			}
			else
			{
				throw new Error("MainUICenter is a single class");
			}

		}

		private function addEvent():void
		{
			/**侦听打开公告面板*/
			dispatcher.addEventListener(EventName.OPEN_GONGGAO, openGonggao);
			/**侦听打开个人信息面板*/
			dispatcher.addEventListener(EventName.OPEN_MESSAGE, openMessage);
			/**侦听购买材料成功*/
			dispatcher.addEventListener(EventName.BUY_SUCCESS,buySuccess);
			/**侦听打开任务面板*/
			dispatcher.addEventListener(EventName.OPEN_TASK,openTask);
			/**侦听打开雇佣面板*/
			dispatcher.addEventListener(EventName.OPEN_GUYONG,openEmployment);
			/**侦听打开厨艺面板*/
			dispatcher.addEventListener(EventName.OPEN_LEARN,openCook);

		}

		private function buySuccess(e:GameEvent):void
		{
			if (ConfigManager.CONNECT_SERVER) 
			{
				/**与服务器交互*/
				NetConnect.getInstance().call(e.param);
			}			
			_mainUI.money=Global.money;
			UIManager.orderMenu.setFoodType(e.param.source, e.param.num);
		}

		private function openGonggao(e:GameEvent):void
		{
			if (!_announceView)
			{
				_announceView=new AnnounceView();
				UIManager.announce=_announceView;
			}
			_announceView.isPop=true;
		}

		private function openMessage(e:GameEvent):void
		{
			if (!_informationView)
			{
				_informationView=new InformationView();
				UIManager.information=_informationView;
			}
			_informationView.isPop=true;
		}

		private function openTask(e:GameEvent):void
		{
			if (!_assignment) 
			{
				_assignment = new Assignment();
				UIManager.assignment = _assignment;
			}
			_assignment.isPop = true;
		}
		
		private function openEmployment(e:GameEvent):void
		{
			if (!_employmentmenu) 
			{
				_employmentmenu = new EmploymentMenu();
				UIManager.employmentmenu = _employmentmenu;
			}
			_employmentmenu.isPop = true;
		}
		
		private function openCook(e:GameEvent):void
		{
			if (!_cookmenu) 
			{
				_cookmenu = new CookingMenu();
				UIManager.cookmenu = _cookmenu;
			}
			_cookmenu.isPop = true;
		}
		
		
		public static function getInstance():MainUICenter
		{
			if (!_instance)
				_instance=new MainUICenter();
			return _instance;
		}


		private function init():void
		{
			_mainUI=new MainUI();
			LayerManager.uiLayer.addChild(_mainUI);
			UIManager.mainUI=_mainUI;


			_mainUI.money=Global.money;
			/**这里顺序不能反转,因为先设置当前经验会以默认的总经验值作为比较对象,然后直接升级归0*/			
			_mainUI.totalEx=Global.totalEx;
			_mainUI.nowEx=Global.nowEx;
			_mainUI.level=Global.level;
			for (var i:int=0; i < Global.foodDataArr.length; i++)
			{
				if (Global.foodDataArr[i] == null) 
				{
					continue;
				}
				UIManager.orderMenu.setFoodType(Global.foodDataArr[i].source, Global.foodDataArr[i].num,i);
			}


		}
	}
}